package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.ButcherAdmissionInspection;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/17.
 *
 */
@Repository
public class ButcherAdmissionInspectionDao extends MyBatisDao<ButcherAdmissionInspection>{

    /**
     *
     * @param offset 页码
     * @param limit 页数
     * @param criteria 参数
     * @return 分页的对象
     */
    public Paging<ButcherAdmissionInspection> pagingByButcherFactoryId(Integer offset, Integer limit, Map<String, Object> criteria) {
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("countByButcherFactoryId"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ButcherAdmissionInspection> data = getSqlSession().selectList(this.sqlId("pagingByButcherFactoryId"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     *
     * @param offset 页码
     * @param limit 页数
     * @param criteria 参数
     * @return 分页的对象
     */
    public Paging<ButcherAdmissionInspection> pagingButcherAdmissionInspection(Integer offset , Integer limit, Map<String ,Object> criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("countAll"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ButcherAdmissionInspection> data = getSqlSession().selectList(this.sqlId("pagingAll"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 根据单据id查找单据的状态
     * @param id 单据的主键id
     * @return status
     */
    public Integer findStatusByID(Long id){
        return getSqlSession().selectOne(sqlId("findStatusById"),id);
    }

    /**
     * 返回当前单据的编码的最大值
     * @param number 除去流水号的前缀
     * @return string
     */
    public String findCurrentMaxNumber(String number){
        return getSqlSession().selectOne(sqlId("findCurrentMaxNumber"),number);
    }

    /**
     * 查询合计 （申报数、合格数、不合格数）
     * @param map 参数
     * @return List<Map<String , BigDecimal>>
     */
    public List<Map<String , BigDecimal>> findSum(Map<String , Object> map){
        return getSqlSession().selectList(sqlId("findSum"),map);
    }

    /**
     * 查询合计 （是否是本地）
     * @param map 参数
     * @return List<Map<String , Integer>>
     */
    public List<Map<String , Integer>> findSumIsLocal(Map<String , Object> map){
        return getSqlSession().selectList(sqlId("findSumIsLocal"),map);
    }

    /**
     * 查询合计 （根据屠宰场）
     * @param map 参数
     * @return List<Map<String , BigDecimal>>
     */
    public List<Map<String , BigDecimal>> findSumButcherFactory(Map<String , Object> map){
        return getSqlSession().selectList(sqlId("findSumButcherFactory"),map);
    }


    public int updateButcherSelected(ButcherAdmissionInspection butcherAdmissionInspection){
        return getSqlSession().update(sqlId("updateButcherSelected"),butcherAdmissionInspection);
    }



    public Paging<ButcherAdmissionInspection> pagingCommon(Integer offset,Integer limit,Map criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("count1"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ButcherAdmissionInspection> data = getSqlSession().selectList(this.sqlId("paging1"),criteria);
            return new Paging<>(total, data);
        }
    }

}